package com.mb.app.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.domain.TbBanner;
import com.mb.app.dao.TbBannerMapper;
import com.mb.app.service.ITbBannerService;
import com.mb.common.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 轮播图  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-04-03
 */
@Service
@Slf4j
public class TbBannerServiceImpl extends ServiceImpl<TbBannerMapper, TbBanner> implements ITbBannerService {

    @Autowired
    private CommonConfig commonConfig;

    @Override
    public List<TbBanner> queryList(Map<String, Object> map) {
        List<TbBanner> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Override
    public int save(TbBanner TbBanner) {
        TbBanner.setCreateTime(new Date());
        if (StringUtils.isNotEmpty(TbBanner.getPicUrl())) {
            String ImgUrl = TbBanner.getPicUrl();
            ImgUrl = ImgUrl.replaceAll(commonConfig.getImgDomain(), "");
            TbBanner.setPicUrl(ImgUrl);
        }
        return this.baseMapper.insert(TbBanner);
    }

    @Override
    public int update(TbBanner TbBanner) {
        try {

            if (StringUtils.isNotEmpty(TbBanner.getPicUrl())) {
                String ImgUrl = TbBanner.getPicUrl();
                ImgUrl = ImgUrl.replaceAll(commonConfig.getImgDomain(), "");
                TbBanner.setPicUrl(ImgUrl);
            }
            TbBanner.setUpdateTime(new Date());
            this.baseMapper.updateById(TbBanner);
        } catch (Exception e) {
            log.error("update Exception", e);
        }
        return 0;
    }


    @Override
    public int remove(Long bannerId, Long delUserId) {
//        return this.baseMapper.deleteById(appId);
        TbBanner TbBanner = new TbBanner();
        TbBanner.setBannerId(bannerId);
        TbBanner.setDeleteTime(new Date());
        TbBanner.setDataStatus(0);
        TbBanner.setDeleteUserId(delUserId);
        return this.baseMapper.updateById(TbBanner);
    }

    @Override
    public TbBanner getDtl(Long bannerId) {
        TbBanner TbBanner = this.baseMapper.selectByIds(bannerId);
        if (TbBanner != null && StringUtils.isNotEmpty(TbBanner.getPicUrl())) {
            TbBanner.setPicUrl(commonConfig.getImgDomain() + TbBanner.getPicUrl());
        }
        return TbBanner;
    }

    @Override
    public boolean channelNameCodeExit(Map<String, Object> params) {
        boolean exit;
        exit = this.baseMapper.selectByBannerInfo(params).size() > 0;
        return exit;
    }

    @Override
    public int status(Long bannerId, Integer status) {
//        return this.baseMapper.deleteById(appId);
        TbBanner TbBanner = new TbBanner();
        TbBanner.setBannerId(bannerId);
//        TbBanner.(status);
        return this.baseMapper.updateById(TbBanner);
    }
}
